//
//  ViewController.m
//  BBAlertViewController_Demo
//
//  Created by admin on 2022/8/8.
//

#import "ViewController.h"
#import "BBAlertViewController.h"
#import "BBAlertConfiguration.h"
#import "BBAlertSheetController.h"
#import "BBAlertAction.h"

#import <Masonry/Masonry.h>

/// 判断是否是流海屏
#define M_G_IsBangsScreen ({\
    BOOL isBangsScreen = NO; \
    if (@available(iOS 11.0, *)) { \
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject]; \
    isBangsScreen = window.safeAreaInsets.bottom > 0; \
    } \
    isBangsScreen; \
})

/// 计算屏幕尺寸比率 基数是 375 *812屏
#define M_G_W_Scale [UIScreen mainScreen].bounds.size.width / 375.f

/// 计算屏幕尺寸比率 基数是 375 *812屏 24是头部与老款手机多出来的尺寸 ， 34是底部安全区域高度
#define M_G_H_Scale ([UIScreen mainScreen].bounds.size.height - 24.f - 34.f) / 812.f

#define M_G_ALL_Scale ({\
CGFloat scale = 1.f;\
BOOL isBangsScreen = NO; \
if (@available(iOS 11.0, *)) { \
UIWindow *window = [[UIApplication sharedApplication].windows firstObject]; \
isBangsScreen = window.safeAreaInsets.bottom > 0; \
} \
if (isBangsScreen) {\
scale = M_G_H_Scale;\
} else {\
scale = [UIScreen mainScreen].bounds.size.height / 812.f;\
}\
scale;\
})

/// 按比率计算尺寸大小。基数是 375 * 812屏
#define M_G_Relative(a)      M_G_Scale * a

/// 适配全设备尺寸大小
#define M_G_All_Relative(a) M_G_ALL_Scale * a

#define M_G_BangsScreen_Height ({\
    CGFloat bangsScreenHeight = 0;\
    if (@available(iOS 13.0, *)) { \
        UIStatusBarManager *statusBarManager = [[UIApplication sharedApplication].windows lastObject].windowScene.statusBarManager;\
        bangsScreenHeight = statusBarManager.statusBarFrame.size.height;\
    }  else { \
        CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];\
        bangsScreenHeight = statusBarFrame.size.height;\
    }\
    bangsScreenHeight;\
})

@interface ViewController ()

@end

@implementation ViewController

- (CGFloat)allDevicesScaleWithDecimal:(CGFloat)decimal {
    CGFloat result = decimal;
    if (M_G_IsBangsScreen) {
        result = M_G_W_Scale * decimal;
    } else {
        result = M_G_H_Scale * decimal;
    }
    return result;
}

- (NSString *)binaryNumberStringFromIntegerNumber:(NSUInteger )number {
    NSMutableString *resultStr = [NSMutableString string];
    if (number == 1 || number == 0) {
        return [NSString stringWithFormat:@"%ld",number];
    }
    //递归、拼接
    while (number / 2) {
        NSUInteger singleNumber = number % 2;
        NSString *singleStr = [NSString stringWithFormat:@"%ld",singleNumber];
        [resultStr insertString:singleStr atIndex:0];
        number = number / 2;
        //最后一位需判断、其实就是最高位1
        if (number / 2 == 0) {
            NSString *devStr = [NSString stringWithFormat:@"%ld",number];
            [resultStr insertString:devStr atIndex:0];
        }
    }
    return resultStr;
}

/**
 十进制转换十六进制

 @param decimal 十进制数
 @return 十六进制数
 */
- (NSString *)hexWithDecimal:(NSInteger)decimal {
    NSString *hex =@"";
    NSString *letter;
    NSInteger number;
    for (int i = 0; i < 9; i++) {
        number = decimal % 16;
        decimal = decimal / 16;
        switch (number) {
            case 10:
                letter = @"A"; break;
            case 11:
                letter = @"B"; break;
            case 12:
                letter = @"C"; break;
            case 13:
                letter = @"D"; break;
            case 14:
                letter = @"E"; break;
            case 15:
                letter = @"F"; break;
            default:
                letter = [NSString stringWithFormat:@"%ld", number];
        }
        hex = [letter stringByAppendingString:hex];
        if (decimal == 0) {
            break;
        }
    }
    return hex;
}


/**
 二进制转换成十六进制

 @param binary 二进制数
 @return 十六进制数
 */
- (NSString *)hexWithBinary:(NSString *)binary {
    NSMutableDictionary *binaryDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [binaryDic setObject:@"0" forKey:@"0000"];
    [binaryDic setObject:@"1" forKey:@"0001"];
    [binaryDic setObject:@"2" forKey:@"0010"];
    [binaryDic setObject:@"3" forKey:@"0011"];
    [binaryDic setObject:@"4" forKey:@"0100"];
    [binaryDic setObject:@"5" forKey:@"0101"];
    [binaryDic setObject:@"6" forKey:@"0110"];
    [binaryDic setObject:@"7" forKey:@"0111"];
    [binaryDic setObject:@"8" forKey:@"1000"];
    [binaryDic setObject:@"9" forKey:@"1001"];
    [binaryDic setObject:@"A" forKey:@"1010"];
    [binaryDic setObject:@"B" forKey:@"1011"];
    [binaryDic setObject:@"C" forKey:@"1100"];
    [binaryDic setObject:@"D" forKey:@"1101"];
    [binaryDic setObject:@"E" forKey:@"1110"];
    [binaryDic setObject:@"F" forKey:@"1111"];
    if (binary.length % 4 != 0) {
        NSMutableString *mStr = [[NSMutableString alloc]init];;
        for (int i = 0; i < 4 - binary.length % 4; i++) {
            [mStr appendString:@"0"];
        }
        binary = [mStr stringByAppendingString:binary];
    }
    NSString *hex = @"";
    for (int i=0; i<binary.length; i+=4) {
        NSString *key = [binary substringWithRange:NSMakeRange(i, 4)];
        NSString *value = [binaryDic objectForKey:key];
        if (value) {
            hex = [hex stringByAppendingString:value];
        }
    }
    return hex;
}

- (void)paomadeng {
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,200,self.view.bounds.size.width*4,100)];

        label3.backgroundColor = [UIColor redColor];

        label3.text =@"噜啦啦噜啦啦噜啦噜啦噜，噜啦噜啦噜啦噜啦噜啦噜~~~";

        [self.view addSubview:label3];

        

        CGRect frame = label3.frame;

        frame.origin.x = 0;

        label3.frame = frame;

        [UIView beginAnimations:@"testAnimation"context:NULL];

        [UIView setAnimationDuration:10.0f];

        [UIView setAnimationCurve:UIViewAnimationCurveLinear];

        [UIView setAnimationDelegate:self];

        [UIView setAnimationRepeatAutoreverses:NO];

        [UIView setAnimationRepeatCount:115];

        frame = label3.frame;

        frame.origin.x =-350;

        label3.frame = frame;

        [UIView commitAnimations];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *string = [self hexWithBinary:@"1100"];
    NSLog(@"binary:0x%@",string);
    
    CGFloat result = M_G_All_Relative(100.f);
    NSLog(@"reslut:%.2f",result);
    result = M_G_All_Relative(18.f);
    NSLog(@"reslut:%.2f",result);
    
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = UIColor.lightGrayColor;
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(M_G_All_Relative(100));
        make.left.right.offset(0);
        make.height.offset(M_G_All_Relative(630));
    }];

    [self.view layoutIfNeeded];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100.f, view1.frame.origin.y, 200.f, 50.f);
    [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = UIColor.redColor;
    [self.view addSubview:button];
    
//   664 -> 812
//
//    CGRect rect = [UIScreen mainScreen].bounds;
//
//
//    UIView *view = [UIView new];
//        view.backgroundColor = [UIColor yellowColor];
//        [self.view addSubview:view];
//
//        [view mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(150);
//            make.left.mas_equalTo(15);
//            make.centerX.mas_equalTo(self.view);
//            make.height.mas_equalTo(50*3);
//        }];
//
//
//        NSMutableArray *tolAry = [NSMutableArray new];//图片数组
//        NSMutableArray *titleAry = [NSMutableArray new];//标题数组
//        NSMutableArray *btnAry = [NSMutableArray new];//按钮数组
//
//        for (int i = 0; i < 4; i ++) {
//            UIImageView *img = [UIImageView new];
//            img.backgroundColor = [UIColor redColor];
//            [view addSubview:img];
//
//            UILabel *lab = [UILabel new];
//            lab.backgroundColor = [UIColor purpleColor];
//            [view addSubview:lab];
//
//            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//            btn.backgroundColor = [UIColor greenColor];
//            [btn setTitle:[NSString stringWithFormat:@"%d",i+90] forState:UIControlStateNormal];
//            [view addSubview:btn];
//
//            [tolAry addObject:img];
//            [titleAry addObject:lab];
//            [btnAry addObject:btn];
//        }
//
//        // 实现masonry垂直方向固定控件高度方法
//          //垂直方向
//          [tolAry mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
//
//          [tolAry mas_makeConstraints:^(MASConstraintMaker *make) {
//
//             //垂直方向可以设置水平居中
//              make.left.mas_equalTo(5);
//              make.width.equalTo(@30);
//          }];
//
//        [titleAry mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
//
//        [titleAry mas_makeConstraints:^(MASConstraintMaker *make) {
//
//           //垂直方向可以设置水平居中
//            make.left.mas_equalTo(40);
//            make.width.equalTo(@60);
//        }];
//
//        [btnAry mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
//
//        [btnAry mas_makeConstraints:^(MASConstraintMaker *make) {
//
//           //垂直方向可以设置水平居中
//            make.right.mas_equalTo(-10);
//            make.width.equalTo(@30);
//        }];
}

- (void)buttonEvent:(id)sender {
//    BBAlertSheetController *alertSheetController = [BBAlertSheetController alertViewControllerWithTitle:@"提供了一个很赞的" withMessage:@"Xcode 提供了一个很赞的方法" withConfiguration:[BBAlertConfiguration defaultAlertSheetConfiguration]];
//    BBAlertAction *confirmAlertAction = [BBAlertAction alertActionWithActionTitle:@"确定" withActionType:BBAlertActionTypeDefault withActionIconImage:[UIImage imageNamed:@"111"] withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
//        NSLog(@"点击确定");
//    }];
//    BBAlertAction *confirmAlertAction1 = [BBAlertAction alertActionWithActionTitle:@"确定" withActionType:BBAlertActionTypeDefault withActionIconImage:[UIImage imageNamed:@"111"] withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
//        NSLog(@"点击确定");
//    }];
//    BBAlertAction *cancelAlertAction1 = [BBAlertAction alertActionWithActionTitle:@"取消" withActionType:BBAlertActionTypeDefault withActionIconImage:[UIImage imageNamed:@"222"] withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
//        NSLog(@"点击取消");
//    }];
//
//    BBAlertAction *cancelAlertAction = [BBAlertAction alertActionWithActionTitle:@"取消" withActionType:BBAlertActionTypeCancel withActionIconImage:[UIImage imageNamed:@"222"] withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
//        NSLog(@"点击取消");
//    }];
//    [alertSheetController addAction:confirmAlertAction1];
//    [alertSheetController addAction:cancelAlertAction1];
//    [alertSheetController addAction:confirmAlertAction];
//    [alertSheetController addAction:cancelAlertAction];
//
//    [self presentViewController:alertSheetController animated:NO completion:nil];
    
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"测试" message:@"Xcode 提供了一个很赞的方法，只需要在 Edit scheme -> Run -> Arguments 中将环境变量 DYLD_PRINT_STATISTICS 设为 1，就可以看到 main 之前各个阶段的时间消耗" preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//
//    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//    [alertController addAction:alertAction2];
//    [alertController addAction:alertAction];
//    [self presentViewController:alertController animated:NO completion:nil];
    
    BBAlertConfiguration *defaultConfiguration = [BBAlertConfiguration defaultAlertViewConfiguration];
//    defaultConfiguration.messageLineCount = 2;
//    defaultConfiguration.alertViewBackgroundColor = UIColor.blueColor;
//    defaultConfiguration.messageColor = UIColor.whiteColor;
//    defaultConfiguration.titleColor = [UIColor brownColor];
//    defaultConfiguration.actionCornerRadius = 7.f;
//    defaultConfiguration.alertViewBackgroundImage = [UIImage imageNamed:@"222"];
    defaultConfiguration.verticalHidden = YES;
    defaultConfiguration.acrossHidden = YES;
//    defaultConfiguration.acrossColor = UIColor.purpleColor;
    defaultConfiguration.actionSpace = 10;
    defaultConfiguration.actionDirection = BBActionDirectionDefault;// BBActionDirectionVertical;//
    defaultConfiguration.typeDefaultBackgroundColor = UIColor.redColor;
    defaultConfiguration.actionEdgeInsets = UIEdgeInsetsMake(0,20, 20, 20);
    defaultConfiguration.typeCancelBackgroundColor = UIColor.redColor;
    defaultConfiguration.actionHeight = 40.f;
    defaultConfiguration.actionCornerRadius = 8.f;
//    defaultConfiguration.typeCancelBackgroundColor = UIColor.blueColor;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(100.f, view1.frame.origin.y, 200.f, 50.f);
    [confirmButton addTarget:self action:@selector(confirmButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.backgroundColor = UIColor.redColor;
//    [self.view addSubview:confirmButton];
    
    BBAlertViewController *alertViewController = [BBAlertViewController alertViewControllerWithTitle:@"提示" withMessage:@"安徽省人民政府门户网站是由安徽省人民政府办公厅主办" withConfiguration:defaultConfiguration];
    alertViewController.customConfirmButton = confirmButton;
    BBAlertAction *confirmAlertAction = [BBAlertAction alertActionWithActionTitle:@"确定" withActionType:BBAlertActionTypeDefault withActionIconImage:nil withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
        NSLog(@"点击确定");
    }];
    BBAlertAction *cancelAlertAction = [BBAlertAction alertActionWithActionTitle:@"取消" withActionType:BBAlertActionTypeCancel withActionIconImage:nil withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
        NSLog(@"点击取消");
    }];

//    [alertViewController addAction:confirmAlertAction];
    [alertViewController addAction:cancelAlertAction];
//    UIView *customView = [[UIView alloc] init];
//    customView.backgroundColor = [UIColor blueColor];
//    alertViewController.customContentView =customView;
//    alertViewController.customLayer = ^{
//        [customView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.offset(0);
//            make.left.offset(10);
//            make.height.offset(200);
//            make.right.bottom.offset(-10);
//        }];
//    };
    
    [self presentViewController:alertViewController animated:NO completion:nil];
    
    BBAlertViewController *alertViewController1 = [BBAlertViewController alertViewControllerWithTitle:@"提示" withMessage:@"安徽省人民政府门户网站是由安徽省1111111111111" withConfiguration:defaultConfiguration];
//    alertViewController1.customConfirmButton = confirmButton;
    BBAlertAction *confirmAlertAction1 = [BBAlertAction alertActionWithActionTitle:@"确定" withActionType:BBAlertActionTypeDefault withActionIconImage:nil withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
        NSLog(@"点击确定");
    }];
    BBAlertAction *cancelAlertAction1 = [BBAlertAction alertActionWithActionTitle:@"取消" withActionType:BBAlertActionTypeCancel withActionIconImage:nil withActionEdgeInsetStyle:BBAlertActionEdgeInsetsStyleLeft withTextAndImageSpace:5.f withActionBlock:^{
        NSLog(@"点击取消");
    }];

    [alertViewController1 addAction:confirmAlertAction1];
    [alertViewController1 addAction:cancelAlertAction1];
    [self presentViewController:alertViewController1 animated:NO completion:nil];
    
    
}

- (void)confirmButtonEvent:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}
@end
